View Javadoc

1   package uba.db.sql.server;
2   
3   import java.io.File;
4   import java.net.InetSocketAddress;
5   import java.util.Iterator;
6   
7   import org.apache.commons.collections.IteratorUtils;
8   
9   /***
10   * Representa la linea de comandos que acepta
11   * {@link uba.db.sql.server.ServerLauncher}.<br>
12   * Los argumentos son: 
13   * <ul>
14   * <li>-h <i>host/direccion IP a escuchar</i></li> 
15   * <li>-p <i>puerto a escuchar</i></li>
16   * <li>-d <i>directorio de la base de datos</li>
17   * </ul>
18   *
19   * Por ejemplo para iniciar el servidor en la linea de comandos se debe
20   * escribir: <code>
21   * java uba.db.sql.server.ServerSession 127.0.0.1 4444
22   * </code>.
23   * Estos parametros de inicio son opcionales y por default valen
24   * {@link #DEFAULT_HOST} y {@link #DEFAULT_PORT}.
25   * 
26   * @version $Revision: 1.2 $
27   */
28  public class ServerCommandLineArguments {
29      /***
30       * Valor por default utilizado para el host del servidor. (127.0.0.1)
31       */
32      public static final String DEFAULT_HOST = "127.0.0.1";
33  
34      /***
35       * Port por default utilizado para escuchar las coneciones (4444).
36       */
37      public static final int DEFAULT_PORT = 4444;
38  
39      private String host;
40      private int port;
41      private File databaseDirectory;
42  
43      public ServerCommandLineArguments(String[] args)
44              throws InvalidServerCommandLineArguments {
45          host = DEFAULT_HOST;
46          port = DEFAULT_PORT;
47          // por default es el directorio actual
48          databaseDirectory = new File(".");
49  
50          Iterator iter = IteratorUtils.arrayIterator(args);
51          while (iter.hasNext()) {
52              String arg = (String) iter.next();
53              
54              if (arg.equals("-h")) {
55                  if (!iter.hasNext()) {
56                      throw new InvalidServerCommandLineArguments("Se esperaba un nombre de host o un ip despues de -h");
57                  }
58                  host = (String) iter.next();
59              } else if (arg.equals("-d")) {
60                  if (!iter.hasNext()) {
61                      throw new InvalidServerCommandLineArguments("Se esperaba un directorio despues de -d");
62                  }
63  
64                  databaseDirectory = new File((String) iter.next());
65              } else if (arg.equals("-p")) {
66                  if (!iter.hasNext()) {
67                      throw new InvalidServerCommandLineArguments("Se esperaba un numero de port despues de -p");
68                  }
69  
70                  arg = (String) iter.next();
71                  try {
72                      port = Integer.parseInt(arg);
73                  } catch (NumberFormatException e) {
74                      throw new InvalidServerCommandLineArguments("El número de port "
75                              + arg + " no es valido");
76                  }
77              }
78          }
79      }
80  
81      public InetSocketAddress serverAddress() {
82          return new InetSocketAddress(host(), port());
83      }
84  
85      public int port() {
86          return port;
87      }
88  
89      public String host() {
90          return host;
91      }
92  
93      public File databaseDirectory() {
94          return databaseDirectory;
95      }
96  }